# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message: 别再使用了,update速度极慢
from base_class import BaseClass

class UpdateMid(BaseClass):

    def __init__(self, **kwargs):
        self.db_config = kwargs.get("db_config")
        self.conn, self.curs = self.get_conn_curs(db_config=self.db_config, type='dict')

    def get_mid_img_dict(self):
        sql = u'''select distinct `mid`,`imgName` from `cars_structure_collection_`'''
        self.curs.execute(sql)
        data = self.curs.fetchall()
        _dict = {i.get("mid"):i.get("imgName") for i in data}
        return _dict

    def get_old_mid_list(self):
        sql = u'''select distinct `mid` from `cars_part_num_new`'''
        self.curs.execute(sql)
        data = self.curs.fetchall()
        _list = [i.get("mid") for i in data]
        return _list

    def run(self):
        # 获取mid,img对照字典
        num = 0
        print("启动...")
        mid_img_dict = self.get_mid_img_dict()
        print("构造对照字典完成")
        old_mid_list = self.get_old_mid_list()
        print("开始循环...")
        for old_mid in old_mid_list:
            new_mid = mid_img_dict.get(old_mid, "")
            if new_mid == "":
                print(old_mid)
                continue
            update_sql = '''update `cars_part_num_new` set `mid`="{}" where `mid`="{}"'''.format(new_mid, old_mid)
            self.curs.execute(update_sql)
            print(update_sql)
            num += 1
            if num % 1000 == 0:
                self.conn.commit()
                print("提交update缓存, 当前数量: {}".format(num))
        self.conn.commit()
        print("最终提交, 总数据量: {}".format(num))


if __name__ == '__main__':
    db_config = dict(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="123456",
        db="nissan",
        charset="utf8",
    )
    tools = UpdateMid(db_config=db_config)
    tools.run()